package com.pudding;


import com.sun.jna.Native;
import com.sun.jna.Structure;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.ptr.IntByReference;
import com.sun.jna.win32.StdCallLibrary;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;


public class Demo9 {

    public interface User32Extended extends StdCallLibrary {
        User32Extended INSTANCE = (User32Extended) Native.load("user32", User32Extended.class);

        void SwitchToThisWindow(HWND hWnd, boolean fAltTab);

        int AllowSetForegroundWindow(int dwProcessId);

        boolean GetClientRect(HWND hWnd, RECT lpRect);

        boolean ClientToScreen(HWND hWnd, WinDef.POINT lpPoint);

        boolean SetCursorPos(int X, int Y);

        void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
    }

    public static class RECT extends Structure {
        public int left, top, right, bottom;

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList("left", "top", "right", "bottom");
        }

        public static class ByReference extends RECT implements Structure.ByReference {
        }
    }

    public static void main(String[] args) {

        // 创建定时任务调度器
        ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

        // 定时任务：每5秒执行一次鼠标点击
        scheduler.scheduleAtFixedRate(() -> {
            try {
                User32 user32 = User32.INSTANCE;
                User32Extended user32Extended = User32Extended.INSTANCE;

                // 替换为你要激活的窗口标题
                String windowTitle = "YDZACXKF01813(c_guxianyang)";

                // 查找窗口
                HWND hwnd = user32.FindWindow(null, windowTitle);
                if (hwnd != null && !hwnd.equals(null)) {

                    // 尝试使用 SwitchToThisWindow 方法
                    user32Extended.SwitchToThisWindow(hwnd, true);

                    // 尝试使用 AllowSetForegroundWindow 方法
                    IntByReference pid = new IntByReference();
                    user32.GetWindowThreadProcessId(hwnd, pid);
                    user32Extended.AllowSetForegroundWindow(pid.getValue());

                    // 激活窗口
                    user32.SetForegroundWindow(hwnd);

                    // 确保窗口获得焦点
                    user32.SetFocus(hwnd);

                    // 延迟一段时间
                    try {
                        Thread.sleep(500); // 延迟 500 毫秒
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    // 获取窗口的客户区矩形
                    RECT.ByReference rect = new RECT.ByReference();
                    user32Extended.GetClientRect(hwnd, rect);

                    // 计算客户区中心点
                    WinDef.POINT clientCenter = new WinDef.POINT(rect.left + rect.right / 2, rect.top + rect.bottom / 2);

                    // 将客户区中心点转换为屏幕坐标
                    user32Extended.ClientToScreen(hwnd, clientCenter);

                    // 移动鼠标到屏幕中心点
                    user32Extended.SetCursorPos(clientCenter.x, clientCenter.y);

                    // 模拟鼠标左键点击
                    final int MOUSEEVENTF_LEFTDOWN = 0x02; // 左键按下
                    final int MOUSEEVENTF_LEFTUP = 0x04;   // 左键释放

                    // 模拟鼠标左键点击
                    user32Extended.mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
                    user32Extended.mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

                    System.out.println("窗口已激活并模拟鼠标点击");
                } else {
                    System.out.println("未找到窗口");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, 0, 500, TimeUnit.SECONDS);

    }
}